//
//  ReportCradAnswerSheetListVC.m
//  jzyParents
//
//  Created by 任辰东 on 2022/9/19.
//

#import "ReportCradAnswerSheetListVC.h"
#import "ReportCradAnswerSheetTCell.h"
#import "XHWebImageAutoSize.h"
#import "GKPhotoBrowser.h"
#import "ReportApis.h"

@interface ReportCradAnswerSheetListVC ()

@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) ReportCardModel *mainModel;
@property (nonatomic, strong) ReportCardKemuModel *kemuModel;

@end

@implementation ReportCradAnswerSheetListVC

-(instancetype)initWithMainModel:(ReportCardModel *)mainModel WithKemuModel:(ReportCardKemuModel *)kemuModel
{
    self = [super init];
    if (self) {
        _mainModel = mainModel;
        _kemuModel = kemuModel;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"科目答题卡";
    [self.view addSubview:self.headerView];
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(10);
    }];
    
    [self.myTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
         
        make.top.equalTo(self.headerView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(kBottomOffset);
    }];

    [self setupEstimatedRowHeight:200 cellClasses:@[[ReportCradAnswerSheetTCell class]]];
    [self beginRefresh];
    
    self.myTableView.mj_header = nil;
    self.myTableView.mj_footer = nil;
}

-(void)requestDataWithOffset:(NSInteger)offset success:(void (^)(NSArray *))success failure:(void (^)(NSString *))failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"main_id"] = self.mainModel.main_id;
    params[@"kemu_id"] = self.kemuModel.kemu_id;
    params[@"id_card"] = [GVUserDefaults standardUserDefaults].id_card;
//    params[@"main_id"] = @"333";
//    params[@"kemu_id"] = @"768";
//    params[@"id_card"] = @"410802200912100020";
    
    WeakSelf
    [self showLoadingHUDWithMessage:nil];
    [ReportApis getStuDatikaListRequestWithParams:params SuccessBlock:^(NSNumber *code, NSString *msg, id responseData) {
        StrongSelf
        [self hideLoadingHUD];
        NSArray *data = [ReportCradAnswerSheetModel mj_objectArrayWithKeyValuesArray:responseData];
        success(data);
        [self addEmptyView];
        
    } FailureBlock:^(NSNumber *code, NSString *msg) {
        StrongSelf
        failure(@"");
        [self showWarningHUDWithMessage:msg completion:NULL];
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReportCradAnswerSheetModel *model = self.dataSourceArr[indexPath.row];
    
    if (model.ti_type.integerValue == 1 || model.ti_type.integerValue == 2) {
        
        return 33 + 20 + 20;
        
    } else {
       
        /**
         *  参数1:图片URL
         *  参数2:imageView 宽度
         *  参数3:预估高度,(此高度仅在图片尚未加载出来前起作用,不影响真实高度)
         */
        CGFloat imageHeight = [XHWebImageAutoSize imageHeightForURL:[NSURL URLWithString:model.ti_answer_img] layoutWidth:kScreenWidth - 30 estimateHeight:200];
        
//        NSString *two = @"";
//        if (!IsEmpty(model.one_score) && !IsEmpty(model.one_teacher_name)) {
//
//            two = [NSString stringWithFormat:@"一评分 %@ %@",model.one_score,model.one_teacher_name];
//        }
//
//        NSString *three = model.one_teacher_time.integerValue == 0 ? @"":[NSString stringWithFormat:@" %@",[model.one_teacher_time dateStringWithFormat:@"yyyy-MM-dd HH:mm:ss"]];
        
        NSString *four = @"";
        if (!IsEmpty(model.two_score) && !IsEmpty(model.two_teacher_name)) {
            
            four = [NSString stringWithFormat:@"\n二评分 %@ %@",model.two_score,model.two_teacher_name];
        }
        
        NSString *five = model.two_teacher_time.integerValue == 0 ? @"":[NSString stringWithFormat:@" %@",[model.two_teacher_time dateStringWithFormat:@"yyyy-MM-dd HH:mm:ss"]];
        
//        NSString *oneStr = [NSString stringWithFormat:@"%@%@",two,three];
        
        NSString *twoStr = [NSString stringWithFormat:@"%@%@",four,five];
        
        CGFloat height = IsEmpty(twoStr) ? 20:40;
        
        return imageHeight + 33 + height + 10 + 20;
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReportCradAnswerSheetTCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ReportCradAnswerSheetTCell class])];
    if(!cell){
        cell = [[ReportCradAnswerSheetTCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([ReportCradAnswerSheetTCell class])];
    }
    ReportCradAnswerSheetModel *model = self.dataSourceArr[indexPath.row];
    [cell configCellWithModel:model];
    
    if (model.ti_type.integerValue == 3 || model.ti_type.integerValue == 4) {
        
    //    WeakSelf
        [cell.contentImageView sd_setImageWithURL:[NSURL URLWithString:model.ti_answer_img] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
    //        StrongSelf
            /**  缓存image size */
            [XHWebImageAutoSize storeImageSize:image forURL:imageURL completed:^(BOOL result) {
                /** reload */
                if(result)  [tableView xh_reloadDataForURL:imageURL];
            }];
        }];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ReportCradAnswerSheetModel *model;
    if (indexPath.row < self.dataSourceArr.count) {
        model = self.dataSourceArr[indexPath.row];
    }
    
    if (model.ti_type.integerValue == 3 || model.ti_type.integerValue == 4) {
        
        DLog(@"点击有图片的");
//        NSArray *imagesArr = [model.ti_answer_img componentsSeparatedByString:@","];
//        NSMutableArray *array = [NSMutableArray array];
//        for (NSString *url in imagesArr) {
//            GKPhoto *photo = [GKPhoto new];
//            photo.url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",url]];
//            [array addObject:photo];
//        }
        GKPhoto *photo = [GKPhoto new];
        photo.url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",model.ti_answer_img]];
        GKPhotoBrowser *browser = [GKPhotoBrowser photoBrowserWithPhotos:@[photo] currentIndex:0];
        [browser showFromVC:self];
    }
    
}

-(void)addEmptyView
{
    if (IsEmpty(self.dataSourceArr)) {
        [RCDEmptyView addEmptyWithStyle:EmptyViewStyleCommon onView:self.myTableView position:EmptyViewPositionCenter clickCallBack:nil];
    }else{
        [RCDEmptyView removeFromParentView:self.myTableView];
    }
}

-(UIView *)headerView
{
    if (!_headerView) {
        _headerView = [UIView new];
        _headerView.backgroundColor = UIColorFromHex(kColorBG);
        [_headerView addTopLine];
        [_headerView addBottomLine];
    }
    return _headerView;
}

@end
